package com.orange.homescreen.pil.defaultimpl.NotificationSummary;

import android.content.ContentResolver;
import android.content.Context;
import android.database.ContentObserver;
import android.net.Uri;
import android.os.Handler;
import android.provider.CallLog.Calls;

import com.orange.homescreen.pil.base.BasePILMonitor;

public class NotificationCallsMonitor extends BasePILMonitor {

	// private final String TAG = "NotificationCallsMonitor";

	public static final Uri	CONTACTS_CALLS_URI	= Calls.CONTENT_URI;

	private boolean			mFlag				= false;
	private ContentResolver	mContactsResolver	= null;
	private ContentObserver	mContactsObserver	= null;

	public NotificationCallsMonitor(Context context) {
		super(context);
	}

	@Override
	public void onPause() {
	}

	@Override
	public void onResume() {
	}

	@Override
	public boolean onStart() {

		// if (mFlag == false) {
		if (!mFlag) {
			mContactsResolver = mContext.getContentResolver();
			mContactsObserver = new ContentObserver(new Handler()) {

				@Override
				public boolean deliverSelfNotifications() {
					return super.deliverSelfNotifications();
				}

				@Override
				public void onChange(boolean selfChange) {
					notifyListeners();
				}

			};

			mContactsResolver.registerContentObserver(CONTACTS_CALLS_URI, true,
					mContactsObserver);
			mFlag = true;
		}
		return true;
	}

	@Override
	public void onStop() {
		if (mContactsResolver != null && mContactsObserver != null) {
			mContactsResolver.unregisterContentObserver(mContactsObserver);
		}
		mContactsResolver = null;
		mContactsObserver = null;
		mFlag = false;
	}
}
